package com.lemon.smartsafetynew.ui.fragment.monitor;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshGridFragment;
import com.lemon.smartsafetynew.entity.monitor.MonitorVideoItemVo;
import com.lemon.smartsafetynew.ui.activity.monitor.MonitorVideoTextureActivity;
import com.lemon.smartsafetynew.ui.adapter.monitor.MonitorVideoItemAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import kale.adapter.LemonRcvAdapter;

/**
 * @deprecated
 * Created by Andrew on 2017/8/7.
 */

public class MonitorVideoFragment extends BasePullToRefreshGridFragment<MonitorVideoItemVo, List<MonitorVideoItemVo>>
        implements RecyclerTClickListener<MonitorVideoItemVo> {

    @Override
    public void onPullRefresh() {
        mPullToRefreshRecyclerView.onRefreshComplete();
    }

    @Override
    public void onLoadMore() {
        mPullToRefreshRecyclerView.setFooterComplete();
    }

    @Override
    public boolean goRefreshOnCreate() {
        return false;
    }

    @Override
    public LemonRcvAdapter<MonitorVideoItemVo> getLemonAdapter() {
        return new MonitorVideoItemAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return "假的，直接取流";
    }


    public static MonitorVideoFragment newInstance() {

        Bundle args = new Bundle();

        MonitorVideoFragment fragment = new MonitorVideoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getSpanCount() {
        return 2;
    }

    @Override
    protected void initViews() {
        super.initViews();
        List<MonitorVideoItemVo> monitorVideoItemVos = getVideoItems();
        mDataList.clear();
        mDataList.addAll(monitorVideoItemVos);
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemClick(MonitorVideoItemVo data, int position) {

        Intent intent = new Intent(getActivity(), MonitorVideoTextureActivity.class);
        intent.putExtra("videoPath", data.getVideoUrl());
        intent.putExtra("titleName", data.getTitle());
        intent.putExtra("mediaCodec", 1);
        intent.putExtra("liveStreaming", 1);
        startActivity(intent);

    }

    public List<MonitorVideoItemVo> getVideoItems() {

/*
        String htmlStr = "{\"total\":6,\"list\":[" +
                "{\"name\":\"jiangnan_yangguang\",\"chineseName\":\"锡师附小 大门\",\"id\":\"6vvEkRzCRheXxfp0l0FQ_A\",\"url\":\"rtsp://admin:admin12345@61.160.121.68:1554/Streaming/Channels/101\"}," +
                "{\"name\":\"furengaozhong_beimen\",\"chineseName\":\"学校北门\",\"id\":\"c90H0w9LTQWKMHo6ERn2iQ\",\"url\":\"rtsp://admin:admin12345@172.22.53.210:554/Streaming/Channels/201\"}," +
                "{\"name\":\"liuzhong_damen\",\"chineseName\":\"学校门口\",\"id\":\"szWpxcQqTyu7_xWIy_O9FA\",\"url\":\"rtsp://admin:admin12345@172.20.71.1:554/Streaming/Channels/101\"}," +
                "{\"name\":\"qingshan_zhongxue\",\"chineseName\":\"学校侧门\",\"id\":\"8j46ntpUSreoctXSUjuZIw\",\"url\":\"rtsp://admin:admin12345@172.22.101.199:554/Streaming/Channels/101\"}," +
                "{\"name\":\"xsfx_jiefanglu\",\"chineseName\":\"锡师附小（解放路）\",\"id\":\"WvDHrPHpRQCyRfY3f3brKA\",\"url\":\"rtsp://admin:admin12345@172.22.237.51:554/Streaming/Channels/101\"}," +
                "{\"name\":\"xsfx_xincheng\",\"chineseName\":\"锡师附小（新城校区）\",\"id\":\"kQeU_SNESz6AVwF2JpJhrA\",\"url\":\"rtsp://admin:admin12345@172.22.237.55:1554/Streaming/Channels/101\"}]}";
*/

        String htmlStr = "{\"total\":3,\"list\":[" +
                "{\"name\":\"jiangnan_yangguang\",\"chineseName\":\"江南中学（阳光校区）\",\"id\":\"6vvEkRzCRheXxfp0l0FQ_A\",\"url\":\"rtsp://admin:admin12345@61.160.121.68:1554/Streaming/Channels/101\"}," +
                "{\"name\":\"xsfx_jiefanglu\",\"chineseName\":\"锡师附小（解放路）\",\"id\":\"WvDHrPHpRQCyRfY3f3brKA\",\"url\":\"rtsp://admin:admin12345@172.22.237.51:554/Streaming/Channels/101\"}," +
                "{\"name\":\"xsfx_xincheng\",\"chineseName\":\"锡师附小（新城校区）\",\"id\":\"kQeU_SNESz6AVwF2JpJhrA\",\"url\":\"rtsp://admin:admin12345@172.22.237.55:1554/Streaming/Channels/101\"}]}";

        String[] urlArrays = new String[] {
                "http://58.215.171.233:18080/hls/jiangnan_yangguang.m3u8",
                "http://58.215.171.233:18080/hls/xsfx_jiefanglu.m3u8",
                "http://58.215.171.233:18080/hls/xsfx_xincheng.m3u8"
        };

        Log.i("haha", htmlStr);

        List<MonitorVideoItemVo> videoItems = new ArrayList<>();
        Map<String, Object> map = JsonUtil.toEntry(htmlStr);
        int totalNumber = (int) map.get("total");
        List<Map<String, Object>> jsonList = (List<Map<String, Object>>) map.get("list");
        for (int i = 0; i < jsonList.size(); i++) {
            MonitorVideoItemVo videoItem = new MonitorVideoItemVo();
            videoItem.setTotalNumber(totalNumber);
            Map<String, Object> itemMap = jsonList.get(i);
            String itemId = (String) itemMap.get("id");
            String container = (String) itemMap.get("name");
            String title = (String) itemMap.get("chineseName");
            videoItem.setId(itemId);
//            videoItem.setVideoUrl("http://anbao.wxjy.com.cn/hls" + "/" + container + "" +
//                    ".m3u8");
            videoItem.setVideoUrl(urlArrays[i]);
            videoItem.setImageUrl("http://anbao.wxjy.com.cn/hls" + "/png/" +
                    container + ".png");
            videoItem.setTitle(title);
            videoItem.setPageNumber(currentPage);
            videoItem.setUpdateTime(new Date());
            videoItems.add(videoItem);
        }

        return videoItems;
    }

    private static class JsonUtil {

        private static ObjectMapper mapper = new ObjectMapper();
        // public static final Log log = LogFactory.getLog(JsonUtil.class);

        public static Map<String, Object> toEntry(String textLine) {
            try {
                JsonParser jsonParser = mapper.getFactory().createParser(textLine);
                @SuppressWarnings("unchecked")
                Map<String, Object> entry = mapper.readValue(jsonParser, Map.class);
                return entry;
            } catch (IOException e) {
                Log.e("JsonUtil", textLine + " is wrong format!", e);
                return null;
            }
        }

        public static <K, V> String toJson(Map<K, V> entry) {
            try {
                String json = mapper.writeValueAsString(entry);
                return json;
            } catch (IOException e) {
                Log.e("JsonUtil", "wrong format!", e);
                return null;
            }
        }

    }
}
